package com.saurshaz.quickfixj.engine;

import quickfix.DoNotSend;
import quickfix.FieldNotFound;
import quickfix.IncorrectDataFormat;
import quickfix.IncorrectTagValue;
import quickfix.Message;
import quickfix.MessageCracker;
import quickfix.RejectLogon;
import quickfix.Session;
import quickfix.SessionID;
import quickfix.SessionNotFound;
import quickfix.UnsupportedMessageType;
import quickfix.field.AvgPx;
import quickfix.field.ClOrdID;
import quickfix.field.CumQty;
import quickfix.field.ExecID;
import quickfix.field.ExecType;
import quickfix.field.LeavesQty;
import quickfix.field.OrdStatus;
import quickfix.field.OrderID;
import quickfix.field.Side;
import quickfix.field.Text;
import quickfix.fix44.ExecutionReport;

public class SenderApplication extends MessageCracker implements
		quickfix.Application {

	public static void main(String[] args) {
		SenderApplication sender = new SenderApplication();
		try {
			sender.sendRejectOrder();
		} catch (SessionNotFound e) {
			e.printStackTrace();
		}

	}

	void sendOrderCancelRequest() throws SessionNotFound {
		// quickfix.fix44.OrderCancelRequest message = new
		// quickfix.fix44.OrderCancelRequest(
		// new OrigClOrdID("123"), new ClOrdID("321"), new Symbol("LNUX"),
		// new Side(Side.BUY));
		//
		// message.set(new Text("Cancel My Order!"));
		//
		// Session.sendToTarget(message, "SENDER", "TARGET");

	}

	public static void sendRejectOrder() throws SessionNotFound {
		/*
		 * quickfix.fix44.Advertisement message = new
		 * quickfix.fix44.Advertisement( new AdvId("123"), new
		 * AdvTransType("321"), new AdvSide(Side.BUY), new Quantity(1000));
		 * 
		 * message.set(new Text(" ----- Advertisementr!"));
		 */

		ExecutionReport fixOrder = new ExecutionReport(new OrderID("123"),
				new ExecID("456"), new ExecType(ExecType.REJECTED),
				new OrdStatus(ExecType.REJECTED), new Side(Side.BUY),
				new LeavesQty(0), new CumQty(0), new AvgPx(0));

		fixOrder.setString(ClOrdID.FIELD, "123");
		fixOrder.setString(Text.FIELD, "Exception mesage detail");

		try {
			Session.sendToTarget(fixOrder, "Foo", "FXCM");
		} catch (SessionNotFound e) {
			e.printStackTrace();
		}

	}

	@Override
	public void onCreate(SessionID sessionId) {
		System.out.println("onCreate");
	}

	@Override
	public void onLogon(SessionID sessionId) {
		System.out.println("onLogon");
	}

	@Override
	public void onLogout(SessionID sessionId) {
		System.out.println("onLogout");
	}

	@Override
	public void toAdmin(Message message, SessionID sessionId) {

		System.out.println("toAdmin");
	}

	@Override
	public void fromAdmin(Message message, SessionID sessionId)
			throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue,
			RejectLogon {
		// TODO Auto-generated method stub
		System.out.println("fromAdmin");
	}

	@Override
	public void toApp(Message message, SessionID sessionId) throws DoNotSend {
		System.out.println("toApp");
		try {
			sendRejectOrder();
		} catch (SessionNotFound e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void fromApp(Message message, SessionID sessionId)
			throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue,
			UnsupportedMessageType {
		System.out.println("fromApp");
	}
}